package list_map;

import java.util.*;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-29 11:28
 * @version: 1.0
 */
public class TreeSetTest {
    public static void main(String[] args) {
//        TreeSet，SortedSet接收可以排序
//        TreeSet<User1> treeSet = new TreeSet<>();
//        SortedSet<User1> treeSet = new TreeSet<>();
//        Set接口 不能排序
        Collection<Integer> set=new TreeSet<>();
        for (int i = 0; i <100 ; i++) {
            set.add(new Random().nextInt(1000));
        }
//        set.forEach(System.out::println);
        for(int n:set){
            System.out.println(n);
        }

        Set<User1> treeSet = new TreeSet<>();
        treeSet.add(new User1(1,6));
        treeSet.add(new User1(3,3));
        treeSet.add(new User1(4,1));
        treeSet.add(new User1(2,8));
//        TreeSet<String> treeSet = new TreeSet<>();
//        treeSet.add("b");
//        treeSet.add("s");
//        treeSet.add("a");
//
//        Iterator<String> iterator = treeSet.iterator();
        Iterator<User1> iterator = treeSet.iterator();
//        String
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
class User1 implements Comparable<User1>{
    int id;
    int age;

    public User1(int id, int age) {
        this.id = id;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User1{" +
                "id=" + id +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User1)) return false;
        User1 user1 = (User1) o;
        return id == user1.id &&
                age == user1.age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, age)+new Random().nextInt();
    }

    @Override
    public int compareTo(User1 o) {
//        System.out.println(this.age-o.age);
        return this.age-o.age;
//        return -1;
    }

}
